package com.rc.saas.tenant.service.invite;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.invite.MemberInviteSettingMapper;
import com.rc.saas.tenant.model.invite.MemberInviteSetting;
import com.rc.saas.tenant.search.invite.InviteSearch;
import com.rc.saas.tenant.vo.invite.InviteSettingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Create by Yang on 2021/4/26
 */
@Service
public class MemberInviteSettingServiceimpl implements MemberInviteSettingService {

    @Autowired
    private MemberInviteSettingMapper memberInviteSettingMapper;

    @Override
    public MemberInviteSettingMapper getMapper() {
        return memberInviteSettingMapper;
    }


    @Override
    public PageInfo<InviteSettingVo> listPageInviteGain(InviteSearch inviteSearch) {
        PageHelper.startPage(inviteSearch.getOffset(), inviteSearch.getLimit());
        List<InviteSettingVo> stationInvoiceVOList = memberInviteSettingMapper.listPageInviteGain(inviteSearch);
        return new PageInfo<>(stationInvoiceVOList);
    }

    @Override
    public PageInfo<InviteSettingVo> listPageInviteTicket(InviteSearch inviteSearch) {
        PageHelper.startPage(inviteSearch.getOffset(), inviteSearch.getLimit());
        List<InviteSettingVo> stationInvoiceVoList = memberInviteSettingMapper.listPageInviteTicket(inviteSearch);
        return new PageInfo<>(stationInvoiceVoList);
    }

    @Override
    public int insertOrUpdateMemberInviteSetting(MemberInviteSetting memberInviteSetting) {

        //停用旧活动
        if (memberInviteSetting.getInviteSettingId() != null) {
            MemberInviteSetting oldMemberInviteSetting = new MemberInviteSetting();
            oldMemberInviteSetting.setInviteSettingId(memberInviteSetting.getInviteSettingId());
            oldMemberInviteSetting.setStatus("0");
            oldMemberInviteSetting.setLastUpdatedTime(new Date());
            memberInviteSettingMapper.updateByPrimaryKeySelective(oldMemberInviteSetting);
            memberInviteSetting.setInviteSettingId(null);
        }

        //停用所有活动
        memberInviteSettingMapper.updateStautsAll();
        //新增新活动
        memberInviteSetting.setLastUpdatedTime(new Date());
        memberInviteSetting.setCreatedTime(new Date());
        memberInviteSetting.setStatus("1");
        memberInviteSettingMapper.insertSelective(memberInviteSetting);
        return 0;
    }

    @Override
    public int updateStatus(Long inviteSettingId) {
        MemberInviteSetting newMemberInviteSetting = new MemberInviteSetting();
        newMemberInviteSetting.setInviteSettingId(inviteSettingId);
        newMemberInviteSetting.setStatus("0");
        newMemberInviteSetting.setLastUpdatedTime(new Date());
        memberInviteSettingMapper.updateByPrimaryKeySelective(newMemberInviteSetting);
        return 0;
    }
}
